<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au" xmlns:rwt="http://schema.pbs.gov.au/RWT/">

  <xsl:import href="common.xsl"/>

  <db:article>
    <db:info>
      <db:title>Extract Restrictions File</db:title>

      <db:copyright>
        <db:year>2013</db:year>
        <db:year>2012</db:year>
        <db:year>2011</db:year>
        <db:year>2009</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").
          The following terms apply to all files associated with the software unless explicitly
          disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this
          software and its documentation for any purpose, provided that existing copyright notices
          are retained in all copies and that this notice is included verbatim in any distributions.
          No written agreement, license, or royalty fee is required for any of the authorized uses.
          Modifications to this software may be copyrighted by their authors and need not follow the
          licensing terms described here, provided that the new terms are clearly indicated on the
          first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
          DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
          THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE
          BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED
          TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
          NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS
          NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
          MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>2.7.1</db:revnumber>
          <db:date>2013-06-04</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Remove xsl:message element.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.7</db:revnumber>
          <db:date>2013-06-02</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Include rwt:prescriber-instruction content.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.6</db:revnumber>
          <db:date>2013-01-21</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added double space to indicate line breaks.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.5</db:revnumber>
          <db:date>2012-11-09</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed problem with deleted restrictions.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.4.2</db:revnumber>
          <db:date>2012-11-07</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added documentation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.4.1</db:revnumber>
          <db:date>2012-06-06</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Update to use latest values for authority methods.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.4</db:revnumber>
          <db:date>2012-05-31</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Collapse whitespace in indication text to prevent tab characters in data
            being interpreted as field delimiters. Truncate indication text to a maximum of 17040
            characters.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.3</db:revnumber>
          <db:date>2011-11-14</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Modified keys to address performance issues</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.2</db:revnumber>
          <db:date>2011-10-21</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed text required.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1.1</db:revnumber>
          <db:date>2011-08-31</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed bug where duplicates were being incorrectly eliminated. Added support
            for auth:all.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1</db:revnumber>
          <db:date>2011-08-01</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Fixed bug where the 'id' key wasn't defined.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0.1</db:revnumber>
          <db:date>2011-06-15</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed blank entries.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2011-02-18</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated for PBS XML Schema v2.4.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2009-10-01</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet produces the Restrictions ANS extract file
        (<db:filename>RestrictionExtract yyyymmdd.txt</db:filename>) from a PBS XML
      document.</db:para>
    <db:para>A restriction is comprised of one or more indications. The Restriction extract file
      lists the Indication Ids and the text of the Indications. This is the text that appears under
      the headings 'Restricted benefit', 'Authority Required (STREAMLINED)' and 'Authority Required'
      in the Schedule of Pharmaceutical Benefits.</db:para>
    <db:para>The source document must be a PBS XML document.</db:para>
    <db:para>The result document is a text file.</db:para>
    <db:para>The field delimeter in this file is the tab character.</db:para>
    <db:table>
      <db:tgroup cols="5" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:colspec colnum="4" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Start Position</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>Indication Id</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>4</db:entry>
            <db:entry>1</db:entry>
            <db:entry>4 digit numeric, commencing with 1000</db:entry>
          </db:row>
          <db:row>
            <db:entry>Restriction Indication Text </db:entry>
            <db:entry>Character</db:entry>
            <db:entry>17040</db:entry>
            <db:entry>6</db:entry>
            <db:entry>text of indication as it appears in the Schedule</db:entry>
          </db:row>
          <db:row>
            <db:entry>Miscellaneous Restriction Flag</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>1</db:entry>
            <db:entry>17047</db:entry>
            <db:entry>
              <db:para>one numeric character - currently only two values:</db:para>
              <db:simplelist>
                <db:member>0 - default value</db:member>
                <db:member>1 - only one course of treatment allowed </db:member>
              </db:simplelist>
            </db:entry>
          </db:row>
          <db:row>
            <db:entry>Date Required Flag</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>1</db:entry>
            <db:entry>17049</db:entry>
            <db:entry>Y/N, Y where a date (e.g. of an x-ray) is required in an authority
              application, otherwise N</db:entry>
          </db:row>
          <db:row>
            <db:entry>Text required flag</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>1</db:entry>
            <db:entry>17051</db:entry>
            <db:entry>Y/N, Y where some text (other than a date) is required in an authority
              application, otherwise N</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
  </db:article>

  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="restriction" match="rwt:restriction" use="pbs:code"/>

  <xsl:key name="ident" match="*" use="@xml:id"/>

  <db:template name="root">
    <db:refpurpose>Check Document Version</db:refpurpose>
    <db:para>Make sure that the source document uses the correct version of the PBS XML
      Schema.</db:para>
  </db:template>
  <xsl:template match="/">
    <xsl:variable name="major" select="substring-before(/*/@version, &quot;.&quot;)"/>

    <xsl:choose>
      <xsl:when test="$major >= 2">
        <!-- Eliminate duplicates -->
        <xsl:apply-templates
          select="//rwt:restriction[count(key(&quot;restriction&quot;, pbs:code)[1]|.) = 1]">
          <xsl:sort select="pbs:code" order="ascending" data-type="number"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">This stylesheet is designed to operate upon a Schedule XML
          document that conforms to version 2.4 (or later) of the PBS XML Schema. See
          http://xml.pbs.gov.au/ for more information on the PBS XML Schema, and for a version of
          this XSL stylesheet that operates on documents that conform to an earlier version of the
          schema.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="rwt:restriction">
    <xsl:variable name="method">
      <xsl:apply-templates
        select="rwt:circumstance | rwt:circumstance-reference"
        mode="method"/>
    </xsl:variable>

    <xsl:if test="$method != '' and rwt:alternate-text">
      <xsl:call-template name="pad-right">
        <xsl:with-param name="text" select="pbs:code"/>
        <xsl:with-param name="length" select="4"/>
      </xsl:call-template>
      <xsl:text>	</xsl:text>
      <xsl:variable name="processedAlternateText">
        <xsl:apply-templates select="rwt:alternate-text" mode="alternate"/>
      </xsl:variable>
      <xsl:call-template name="truncate">
        <xsl:with-param name="length" select="17040"/>
        <xsl:with-param name="text">
          <xsl:call-template name="pad-right">
            <xsl:with-param name="text" select="$processedAlternateText"/>
            <xsl:with-param name="length" select="17040"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
      <xsl:text>	</xsl:text>
      <!-- Misc restriction flag -->
      <xsl:variable name="misc">
        <xsl:apply-templates
          select="rwt:circumstance |
                  rwt:circumstance-reference |
                  rwt:administrative-advice |
                  rwt:administrative-advice-reference"
          mode="misc"/>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="$misc != ''">1</xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
      <xsl:text>	</xsl:text>
      <!-- Date required flag -->
      <xsl:variable name="date">
        <xsl:apply-templates
          select="rwt:circumstance |
                  rwt:circumstance-reference |
                  rwt:administrative-advice |
                  rwt:administrative-advice-reference"
          mode="date"/>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="$date != ''">Y</xsl:when>
        <xsl:otherwise>N</xsl:otherwise>
      </xsl:choose>
      <xsl:text>	</xsl:text>
      <!-- Text required flag -->
      <xsl:variable name="text">
        <xsl:apply-templates
          select="rwt:circumstance |
                  rwt:circumstance-reference |
                  rwt:administrative-advice |
                  rwt:administrative-advice-reference"
          mode="text"/>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="$text != ''">Y</xsl:when>
        <xsl:otherwise>N</xsl:otherwise>
      </xsl:choose>
      <xsl:text>&#xa;</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="rwt:circumstance-reference" mode="method">
    <xsl:apply-templates select="key('ident', substring(@xlink:href, 2))" mode="method"/>
  </xsl:template>
  <xsl:template match="rwt:circumstance" mode="method">
    <xsl:if
      test="rwt:authority-method[. = 'auth:restricted' or
                  . = 'auth:all' or
                  . = 'auth:in-writing' or
                  . = 'auth:telephone' or
                  . = 'auth:electronic' or
                  . = 'auth:emergency' or
		              . = 'auth:streamlined']"
      >1</xsl:if>
  </xsl:template>

  <xsl:template match="rwt:circumstance" mode="misc">
    <xsl:apply-templates
      select="rwt:administrative-advice |
                                 rwt:administrative-advice-reference"
      mode="misc"/>
  </xsl:template>
  <xsl:template match="rwt:administrative-advice" mode="misc">
    <xsl:if test="rwt:constraints/rwt:single-treatment">1</xsl:if>
  </xsl:template>
  <xsl:template
    match="rwt:administrative-advice-reference |
                       rwt:circumstance-reference"
    mode="misc">
    <xsl:apply-templates select="key('ident', substring(@xlink:href, 2))" mode="misc"/>
  </xsl:template>
  <xsl:template match="rwt:circumstance" mode="date">
    <xsl:apply-templates
      select="rwt:administrative-advice |
                                 rwt:administrative-advice-reference"
      mode="date"/>
  </xsl:template>
  <xsl:template match="rwt:administrative-advice" mode="date">
    <xsl:if test="rwt:constraints/rwt:date-required">1</xsl:if>
  </xsl:template>
  <xsl:template
    match="rwt:administrative-advice-reference |
                       rwt:circumstance-reference"
    mode="date">
    <xsl:apply-templates select="key('ident', substring(@xlink:href, 2))" mode="date"/>
  </xsl:template>
  <xsl:template match="rwt:circumstance" mode="text">
    <xsl:apply-templates
      select="rwt:administrative-advice |
                                 rwt:administrative-advice-reference"
      mode="text"/>
  </xsl:template>
  <xsl:template match="rwt:administrative-advice" mode="text">
    <xsl:if test="rwt:constraints/rwt:text-required">1</xsl:if>
  </xsl:template>
  <xsl:template
    match="rwt:administrative-advice-reference |
                       rwt:circumstance-reference"
    mode="text">
    <xsl:apply-templates select="key('ident', substring(@xlink:href, 2))" mode="text"/>
  </xsl:template>

  <xsl:template match="rwt:alternate-text" mode="alternate">
    <xsl:apply-templates
      select="*[not(self::db:note[not(starts-with(@role, 'rwt:prescriber-instruction'))]|self::db:caution)]"
      mode="alternate"/>
  </xsl:template>
  <xsl:template match="*" mode="alternate">
    <!-- Text output uses double space to indicate line break and
         triple space to indicate paragraph break.
      -->
    <xsl:choose>
      <xsl:when test="self::db:para and
        starts-with(., ' ') and
        (parent::rwt:alternate-text or parent::db:listitem) and
        preceding-sibling::*[not(self::pbs:code)]">
        <!-- avoid having too many spaces -->
        <xsl:text> </xsl:text>
      </xsl:when>
      <xsl:when test="self::db:para and
        (parent::rwt:alternate-text or parent::db:listitem) and
        preceding-sibling::*[not(self::pbs:code)]">
        <xsl:text>  </xsl:text>
      </xsl:when>
      <xsl:when test="(parent::rwt:alternate-text or parent::db:listitem) and
        preceding-sibling::*[not(self::pbs:code)]">
        <xsl:text> </xsl:text>
      </xsl:when>
    </xsl:choose>
    <xsl:apply-templates mode="alternate"/>
  </xsl:template>
  <xsl:template match="db:listitem" mode="alternate">
    <xsl:choose>
      <xsl:when test="parent::db:itemizedlist">
        <xsl:text> * </xsl:text>
      </xsl:when>
      <xsl:when test="parent::db:orderedlist">
        <xsl:text> </xsl:text>
        <xsl:choose>
          <xsl:when test="count(ancestor::db:orderedlist) mod 3 = 1">
            <xsl:number format="(1)"/>
          </xsl:when>
          <xsl:when test="count(ancestor::db:orderedlist) mod 3 = 2">
            <xsl:number format="(a)"/>
          </xsl:when>
          <xsl:when test="count(ancestor::db:orderedlist) mod 3 = 0">
            <xsl:number format="(i)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:number/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:text> </xsl:text>
      </xsl:when>
    </xsl:choose>
    <xsl:apply-templates mode="alternate"/>
  </xsl:template>

</xsl:stylesheet>
